<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Html_Filter_Abstract **/
require_once 'Zead/Html/Filter/Abstract.php';

/**
 * @category   Zead
 * @package    Zead_Html
 * @subpackage Zead_Html_Filter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
class Zead_Html_Filter_Glossary extends Zead_Html_Filter_Abstract
{
	/**
	 * Aplica o filtro que tem a função de procurar elementos com o id siteName ou outro
	 * passado como argumento que será o título do site.
	 * @param string $src
	 * @param string $selector
	 */
	public function filter($glossaryList) 
	{		
		// Order the glossary list
		$newList = array();
				
		foreach ($glossaryList as $entry) {
			$letter = ucfirst((string) $entry->term);
			$letter = $letter[0];
			
			if (!isset($newList[$letter])) {
				$newList[$letter] = array();
			}
			$newList[$letter][(string) $entry->term] = $entry->definition;
			array_multisort($newList[$letter], SORT_DESC);
		}
		array_multisort($newList, SORT_ASC);
	
		// Creating the glossary html list
		foreach ($this->html->documentElement->select('code.glossary') as $code) {
			$block = $this->html->createElement('div');
			$code->parentNode->replaceChild($block, $code);
			$block->addClassName('glossary');
			$ul = $block->appendChild($this->html->createElement('dl'));
			foreach ($newList as $letter => $entries) {
				$li = $ul->appendChild($this->html->createElement('dt'));
				$li->addClassName('letter');
				$li->nodeValue = $letter;
				$li = $ul->appendChild($this->html->createElement('dd'));
				$dl = $li->appendChild($this->html->createElement('dl'));				
				foreach ($entries as $entry => $definition) {
					$dt = $dl->appendChild($this->html->createElement('dt'));
					$dt->nodeValue = $entry;
					$dd = $dl->appendChild($this->html->createElement('dd'));
					$dd->nodeValue = $definition;
				}
			} 
		}
	}
}

